package com.zlkj.pro.core.util;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * @author 孙铭泽
 * @date 2022/6/20
 * @time 16:15
 */
public class SendEmailUtil {
    // 邮箱smtp协议 这里是使用阿里云的
    public static final String MY_EMAIL_SMTP_HOST = "smtp.gmail.com";

    public static void sendEmail() throws Exception {
        String host = "smtp.gmail.com";
        String from = "zlkjjiangsu@gmail.com";
        String pass = "zlpro";
        Properties props = System.getProperties();
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user", from);
        props.put("mail.smtp.password", pass);
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");
        String[] to = {"sunmingze_zl@163.com"};
        Session session = Session.getDefaultInstance(props, null);
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        InternetAddress[] toAddress = new InternetAddress[to.length];
        for (int i = 0; i < to.length; i++) {
            toAddress[i] = new InternetAddress(to[i]);
        }
        for (InternetAddress address : toAddress) {
            message.addRecipient(Message.RecipientType.TO, address);
        }
        message.setSubject("sending in a group");
        message.setText("Welcome to JavaMail");
        Transport transport = session.getTransport("smtp");
        transport.connect(host, from, pass);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }

    public static void main(String[] args) throws Exception {
        sendEmail();
    }
}
